<template>
  <div class="wrapper">
    <div style="margin: 200px auto;background-color: #fff;width: 350px;height: 300px;padding: 20px;border-radius: 10px">
      <div style="margin: 20px 0;text-align: center;font-size: 24px"><b>登录</b></div>
      <el-form @keyup.enter.native="login" :model="user" :rules="rules" ref="userForm">
        <el-form-item prop="userName">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-user" v-model:prefix-icon="user.managerName"></el-input>
        </el-form-item>
        <el-form-item prop="userPassword">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password v-model:prefix-icon="user.managerPassword"></el-input>
        </el-form-item>
        <div style="margin: 10px 0;text-align: right">
          <el-button type="primary" size="small" autocomplete="off" @click="login" :disabled="!canSubmit" :loading="isLoding">登录</el-button>
          <!--      <el-button type="warning" size="small" autocomplete="off" @click="login">登录</el-button>-->
        </div>
      </el-form>

    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data(){
    return {
      user:{},
      //登录按钮正处于加载过程中，在此过程中不能再次点击
      isLoding:false,
      rules:{
        // 这里我已经设置不输入用户名和密码就无法点击登录按钮，暂时校验用处不大；
        managerName:[
          {required: true, message: '请输入用户名', trigger: 'blur'},
          { min: 3, max: 8, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        managerPassword:[
          {required: true, message: '请输入密码', trigger: 'blur'},
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed:{
    canSubmit(){
      const {managerName,managerPassword} = this.user
      return Boolean(managerName && managerPassword)
    }
  },
  methods:{
    login(){
      this.isLoding = true
      //在提交请求前先对form进行校验
      this.$refs['userForm'].validate((valid) => {
        if (valid) {
          this.request.post("/manager/visitor/managerLogin",this.user).then(res=>{
            if(res.code == 0){
              localStorage.setItem('Token',res.data.tokenInfo.accessToken)
              localStorage.setItem('userId',res.data.tokenInfo.userId)
              localStorage.setItem('userName',res.data.userName)
              this.$message.success("登陆成功")
              this.$router.push("/")
            }else{
              this.$message.error(res.message)
              this.isLoding = false
            }
          })
        } else {
          console.log('error submit!!');
          this.isLoding = false
          return false;
        }
      })

    }

  }
}
</script>

<style scoped>
  .wrapper{
    height: 100vh;
    background-image: linear-gradient(to bottom right,#FC466B, #3F5EFB);
    overflow: hidden;
  }
</style>